﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Home.Web.Models.Resume;
using Home.Web.Data;

namespace Home.Web.Controllers
{
    [RoutePrefix("api")]
    public class SkillTypeController : ApiController
    {
        private ResumeDbContext db = new ResumeDbContext();

        [Route("SkillTypes")]
        // GET api/SkillType
        public IQueryable<SkillType> GetSkillTypes()
        {
            return db.SkillTypes;
        }

        // GET api/SkillType/5
        [ResponseType(typeof(SkillType))]
        public async Task<IHttpActionResult> GetSkillType(int id)
        {
            SkillType skilltype = await db.SkillTypes.FindAsync(id);
            if (skilltype == null)
            {
                return NotFound();
            }

            return Ok(skilltype);
        }

        // PUT api/SkillType/5
        public async Task<IHttpActionResult> PutSkillType(int id, SkillType skilltype)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != skilltype.Id)
            {
                return BadRequest();
            }

            db.Entry(skilltype).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SkillTypeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/SkillType
        [Route("SkillType", Name = "POST SkillType")]
        [ResponseType(typeof(SkillType))]
        public async Task<IHttpActionResult> PostSkillType(SkillType skilltype)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.SkillTypes.Add(skilltype);
            await db.SaveChangesAsync();

            return CreatedAtRoute("POST SkillType", new { id = skilltype.Id }, skilltype);
        }

        // DELETE api/SkillType/5
        [ResponseType(typeof(SkillType))]
        public async Task<IHttpActionResult> DeleteSkillType(int id)
        {
            SkillType skilltype = await db.SkillTypes.FindAsync(id);
            if (skilltype == null)
            {
                return NotFound();
            }

            db.SkillTypes.Remove(skilltype);
            await db.SaveChangesAsync();

            return Ok(skilltype);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool SkillTypeExists(int id)
        {
            return db.SkillTypes.Count(e => e.Id == id) > 0;
        }
    }
}